/*
*       A simple 2D game map editor
*
*       This library is free software; you can redistribute it and/or
*       modify it under the terms of the GNU Lesser General Public
*       License as published by the Free Software Foundation; either
*       version 2.1 of the License, or (at your option) any later version.
*/
// Name        : tilesetwidget
// File        : widget_tileset.h
// Description :
// Author      : hoha
// Date        : 01/07/2009
// Evolutions  : 01/07/2009, Version V0, Creation
#ifndef WIDGET_TILESET_H
#define WIDGET_TILESET_H

#include <QWidget>
#include <QtGui/QWidget>
#include <QPainter>
#include <QImage>
#include <QPixmap>
#include <QModelIndex>
#include <QMouseEvent>
#include <QString>
#include <QPoint>
#include <QRect>
#include <QFileDialog>
#include <QLine>
#include "com_def.h"
#include "com_process.h"

//tile definition
#define TILE_WIDTH 64
#define TILE_HEIGHT 32
//Grid
#define GRID_CELL_WIDTH TILE_WIDTH
#define GRID_CELL_HEIGHT TILE_HEIGHT
#define GRID_H_NUM 20
#define GRID_V_NUM 20
#define TILE_MAX_NUM ((GRID_H_NUM)*(GRID_V_NUM))
//tileset entry,stored in file
typedef struct{
    bool isValid;
    u16 tile_width;
    u16 tile_height;
    u32 tile_size;
    u32 data_offset;
}tileset_entry_t;

//tileset entry,runtime used
typedef struct{
    tileset_entry_t entry;
    unsigned char *raw_data_p;
    QPixmap *img_data_p;
    u32 draw_pos_x;
    u32 draw_pos_y;
}tileset_entry_mem_t;

//tileset file header
typedef struct{
    u16 tile_num;
}tileset_hdr_mem_t;

//tileset
typedef struct{
    tileset_hdr_mem_t head;
    tileset_entry_mem_t mem_entry[TILE_MAX_NUM];
}tileset_mem_t;

extern tileset_mem_t* memtileset;
extern int locked_tile;

class Wt_TileSet : public QWidget
{
       Q_OBJECT
public:
    Wt_TileSet(QWidget *parent = 0);
    ~Wt_TileSet();
    virtual void paintEvent(QPaintEvent *event);
    virtual void mousePressEvent(QMouseEvent *event);
public:
        bool loadFile(QString filename);
        bool saveFile(QString filename);
        bool addTile();
        bool delTile();
public:
    void init_grid(u16 h_cell_num, u16 v_cell_num);
    bool init_tileset_mem(tileset_mem_t* ts);
    bool load_img_data_to_fill(const char* file_path, tileset_mem_t* ts, u16 index);
public:
        //
    u16 tileset_width;
    u16 tileset_height;
        //
    tileset_mem_t *tileset;
    //grid
    u16   grid_line_number;
    QLine *grid;
public:
    u16 selected_tile_index;
    u16 max_value_of_used_index;
    QRect selected_tile_rect;
private:
    u16 tile_current_index;
};
#endif // WIDGET_TILESET_H
